/**
 * 筛选器状态管理
 */
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useFilterStore = defineStore('filter', () => {
  // 筛选条件
  const filters = ref({
    cities: [],
    categories: [],
    big_categories: [],
    company_sizes: [],
    finance_stages: [],
    work_years: [],
    educations: [],
    salary_range: [0, 100], // 薪资范围
    keyword: ''
  })

  // 是否有筛选条件
  const hasFilters = computed(() => {
    return (
      filters.value.cities.length > 0 ||
      filters.value.categories.length > 0 ||
      filters.value.big_categories.length > 0 ||
      filters.value.company_sizes.length > 0 ||
      filters.value.finance_stages.length > 0 ||
      filters.value.work_years.length > 0 ||
      filters.value.educations.length > 0 ||
      filters.value.salary_range[0] > 0 ||
      filters.value.salary_range[1] < 100 ||
      filters.value.keyword !== ''
    )
  })

  // 设置筛选条件
  function setFilter(key, value) {
    filters.value[key] = value
  }

  // 设置多个筛选条件
  function setFilters(newFilters) {
    filters.value = { ...filters.value, ...newFilters }
  }

  // 重置筛选条件
  function resetFilters() {
    filters.value = {
      cities: [],
      categories: [],
      big_categories: [],
      company_sizes: [],
      finance_stages: [],
      work_years: [],
      educations: [],
      salary_range: [0, 100],
      keyword: ''
    }
  }

  // 获取筛选参数（用于API请求）
  function getFilterParams() {
    const params = {}

    if (filters.value.cities.length > 0) {
      params.city = filters.value.cities.join(',')
    }
    if (filters.value.categories.length > 0) {
      params.category = filters.value.categories.join(',')
    }
    if (filters.value.big_categories.length > 0) {
      params.big_category = filters.value.big_categories.join(',')
    }
    if (filters.value.company_sizes.length > 0) {
      params.company_size = filters.value.company_sizes.join(',')
    }
    if (filters.value.finance_stages.length > 0) {
      params.finance_stage = filters.value.finance_stages.join(',')
    }
    if (filters.value.work_years.length > 0) {
      params.work_year = filters.value.work_years.join(',')
    }
    if (filters.value.educations.length > 0) {
      params.education = filters.value.educations.join(',')
    }
    if (filters.value.salary_range[0] > 0) {
      params.salary_min = filters.value.salary_range[0]
    }
    if (filters.value.salary_range[1] < 100) {
      params.salary_max = filters.value.salary_range[1]
    }
    if (filters.value.keyword) {
      params.search = filters.value.keyword
    }

    return params
  }

  return {
    filters,
    hasFilters,
    setFilter,
    setFilters,
    resetFilters,
    getFilterParams
  }
})

